package com.example.bysj_sb.dao;


import com.example.bysj_sb.domain.authority.RolePermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository("rolePermissionDao")
public class RolePermissionDao {
    @Autowired
    private  JdbcTemplate jdbcTemplate;
    //根据roleId返回permissionId
    public  List<Integer> getPermissionId(Integer roleId){
        String sql = "SELECT * FROM RolePermission WHERE role_id = ?";
        List<Integer> permissionIds = jdbcTemplate.query(sql, new Object[]{roleId}, new RowMapper() {
            @Override
            public Object mapRow(ResultSet resultSet, int i) throws SQLException {
                Integer permissionId = resultSet.getInt("permission_id");

                return permissionId;
            }
        });
        return permissionIds;

    }
    public Boolean add(RolePermission rolePermission) {
        String sql = "INSERT INTO RolePermission (role_id,permission_id) VALUES (?,?)";
        int affectedRowNum = jdbcTemplate.update(sql, rolePermission.getRole().getId(),rolePermission.getPermission().getId());
        if (affectedRowNum != 1) {
            return false;
        } else {
            return true;
        }
    }
}
